//
//  NetworkTool.m
//  HealthyMall
//
//  Created by imax on 2017/6/10.
//  Copyright © 2017年 白沙科技. All rights reserved.
//

#import "NetworkRequestTool.h"
#import "SVProgressHUD.h"

@implementation NetworkRequestTool

//网络工具的类方法,单例模式
+ (instancetype)sharedNetworkTool {
    static NetworkRequestTool *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        self.requestSerializer = [AFJSONRequestSerializer serializer];
        self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html",@"application/x-javascript",@"multipart/form-data",nil];
        [self.requestSerializer setTimeoutInterval:15];
    }
    return self;
}
//GET
- (void)GET:(NSString *)URLString parameters:(id)parameters finished:(CGRequestCallBack)finished{
    
    [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"status"] isEqualToString:@"true"]) {
            finished(responseObject, nil);
        }else{
            [SVProgressHUD showImage:nil status:responseObject[@"info"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"++++%@", error);
        [SVProgressHUD showImage:nil status:@"请检查您的网络连接!"];
    }];
}
//POST
- (void)POST:(NSString *)URLString parameters:(id)parameters finished:(CGRequestCallBack)finished{
    
    [self POST:URLString parameters:parameters constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //NSLog(@"====%@",responseObject);
        if ([responseObject[@"status"] isEqualToString:@"true"]) {
            finished(responseObject, nil);
        }else{
            //NSLog(@"====%@",responseObject[@"info"]);
            if ([responseObject[@"info"] isEqualToString:@"身份验证失效，请重新登陆"]) {
                [MyTool logout];
            }
            [SVProgressHUD showImage:nil status:responseObject[@"info"]];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"---%@",error);
        [SVProgressHUD showImage:nil status:@"请检查您的网络连接!"];
    }];
}

//上传图片
- (void)uploadWithURL:(NSString *)URL parameters:(NSDictionary *)parameters images:(NSArray<UIImage *> *)images name:(NSString *)name fileName:(NSString *)fileName mimeType:(NSString *)mimeType progress:(HttpProgress)progress success:(CGRequestCallBack)finished failure:(HttpRequestFailed)failure{
    
    [self POST:URL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        //压缩-添加-上传图片
        for (int i = 0; i < images.count; i ++) {
            NSData * imageData = UIImageJPEGRepresentation(images[i], 0.5);
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
            [formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:mimeType];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        //上传进度
        progress ? progress(uploadProgress) : nil;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        finished(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"++++%@", error);
        failure ? failure(error) : nil;
    }];
}

@end
